#include <functional>
#include <iostream>
#include <algorithm>
#include <vector>

using std::cout;
using std::endl;
using std::remove_if;
using std::for_each;
using std::vector;
using std::bind1st;
using std::bind2nd;
using std::less;
using std::greater;

bool func(int value)
{
    return value > 5;
}

void test()
{
    vector<int> vec = {1, 4, 6, 8, 3, 5, 7, 9, 3};
    for_each(vec.begin(), vec.end(), [](int &value){
             cout << value << "  ";
             });
    cout << endl;

    cout << endl << "执行remove_if之后的结果" << endl;

    //这两行可以删除大于5的元素
    /* auto it = remove_if(vec.begin(), vec.end(), bind1st(less<int>(), 5)); */
    auto it = remove_if(vec.begin(), vec.end(), bind2nd(greater<int>(), 5));

    //这一行只能删除小于5的元素
    /* auto it = remove_if(vec.begin(), vec.end(), bind1st(greater<int>(), 5)); */


    
    //remove_if不能一次将满足条件的元素删除，但是会返回一个待删除元素的首
    //迭代器，最后结合vector的erase进行删除即可
    /* auto it = remove_if(vec.begin(), vec.end(), [](int value){ */
    /*                     return value > 5; */
    /*                     }); */
    /* auto it = remove_if(vec.begin(), vec.end(), func); */
    vec.erase(it, vec.end());

    for_each(vec.begin(), vec.end(), [](int &value){
             cout << value << "  ";
             });
    cout << endl;
}

int main(int argc, char *argv[])
{
    test();
    return 0;
}

